package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SetmealDao_lzl;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService_lzl;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService_lzl.class)
public class SetmealServiceImpl_lzl implements SetmealService_lzl {
    @Autowired
    private SetmealDao_lzl setmealDao_lzl;
//    @Autowired
//    private JedisPool jedisPool;

    /*
     * 添加套餐管理
     * */
    @Override
    public void add(Integer[] ids, Setmeal setmeal) {
        setmealDao_lzl.add(setmeal);
        System.out.println(setmeal.getId());
//        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        addsetmealAndcheckgroup(ids,setmeal);

    }
    /*
    * 分页查询
    * */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Setmeal> page = setmealDao_lzl.findPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }
    /*
     * 根据id，查询setmeal对象，并进行返回
     * */
    @Override
    public Setmeal findByid(Integer id) {
        return setmealDao_lzl.findByid(id);
    }
    /*
     * 根据SetmealId 查询CheckgroupIds
     * */
    @Override
    public List<Integer> findCheckgroupIdsBySetmealId(Integer id) {
        return setmealDao_lzl.findCheckgroupIdsBySetmealId(id);
    }
    /*
     * 修改套餐管理
     * */
    @Override
    public void update(Setmeal setmeal,Integer[] ids) {
        setmealDao_lzl.update(setmeal);
        setmealDao_lzl.deleteCheckGroupIdsBySetmealId(setmeal.getId());
//        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
        addsetmealAndcheckgroup(ids,setmeal);
    }
    /*
    * 根据id  删除
    * */
    @Override
    public void delete(Integer id) throws RuntimeException {
        List<Integer> integerList = setmealDao_lzl.findCheckgroupIdsBySetmealId(id);
        if(integerList!=null&&integerList.size()>0){
          throw   new RuntimeException();
        }else {
            setmealDao_lzl.delete(id);
        }

    }
    /*
    * 查詢所有套餐
    * */
    @Override
    public List<Setmeal> getSetmeal() {
      return   setmealDao_lzl.getSetmeal();

    }
    /*
     * 根据id查詢setmeal，在跟id查询checkGroup
     * */
    @Override
    public Setmeal findById(Integer id) {
      return   setmealDao_lzl.findById(id);
    }
    /*
    * 查询套餐数据统计报表
    * */
    @Override
    public Map<String, Object> getSetmealReport() {
        List<Map> mapList = setmealDao_lzl.getSetmealNameAndOrderCount();
        List<Object> names = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        if(mapList!=null&&mapList.size()>0){
            for (Map map : mapList) {
                String name = (String) map.get("name");
                names.add(name);
            }

            map1.put("setmealNames",names);
            map1.put("setmealCount",mapList);
        }
        return map1;
    }

    /*
    * 添加到中间表
    * */
    public void addsetmealAndcheckgroup(Integer[] ids,Setmeal setmeal){
        if(ids!=null){
            for (Integer id : ids) {
                Map<String, Integer> map = new HashMap<>();
                map.put("setmeal_id",setmeal.getId());
                map.put("checkgroup_id",id);
                setmealDao_lzl.addsetmealAndcheckgroup(map);
            }
        }
    }
}
